import { createRouter, createWebHashHistory } from "vue-router";
import yunshang from "../components/yunshang.vue";
import yisuo from "../components/yisuo.vue";
import MobileLayout from "../views/MobileLayout.vue";


const router = createRouter({
  history: createWebHashHistory(),
  routes: [{ // 修改此处
    path: "/yunshang",
    component: yunshang,
  }, {
    path: "/yisuo",
    component: yisuo,
  },
  {
    path: "/login",
    name: "login",
    component: () => import("../views/Login.vue")
  },
  {
    path: "/app2",
    name: "app2",
    component: () => import("../App2.vue")
  },
  {
    path: "/app3",
    name: "app3",
    component: () => import("../App3.vue")
  },
  // {
  //   path: "/console/uploadView",
  //   name: "console.uploadView",
  //   component: () => import("../App3.vue")
  // },
  {
    path: "/mobile",
    name: "mobile",
    component: () => import("../views/MobileLayout.vue")
  },
  {
    path: "/404",
    name: "404",
    component: () => import("../views/404NotFound.vue")
  },
  { path: "/", redirect: "/login" },
  { path: "/:catchAll(.*)", redirect: "/login" },
  {
    path: "/home",
    name: "home",
    component: () => import("../views/Home.vue")
  },
  {
    path: "/console",
    name: "console",
    component: () => import("../views/Console.vue"),
    children:[
      {
        path: "uploadView",
        name: "console.uploadView",
        component: () => import("../App3.vue")
      },
      {
        path: "uploadView2",
        name: "console.uploadView2",
        component: () => import("../fileComponents/fileUpload.vue")
      }
    ]
  },
  {
    path: "/profile",
    name: "profile",
    component: () => import("../views/profile.vue")
  },

  ]
});
// 全局前置守卫

router.beforeEach((to, from, next) => {
  const userId = localStorage.getItem('userId');
  const token = localStorage.getItem('token'); // 从localStorage获取token
  const isMobile = /iPhone|iPad|iPod|Android/i.test(navigator.userAgent);

  const publicPaths = ['/login', "/home"];
  if (publicPaths.includes(to.path)) {
    next();
  } else if (token) {
    if (to.name === "home" || to.name === "console" || to.name === "profile"|| to.name === "console.uploadView"|| to.name === "console.uploadView2") {
      next();
    } else if (isMobile) {
      next({ name: 'mobile' });
    } else {
      next({ name: "console" });
    }
  } else {
    next({ name: 'login' });
  }
});



export default router;



// router.beforeEach((to, from, next) => {
//   const userId = localStorage.getItem('userId');
//   const token = localStorage.getItem('token'); // 从localStorage获取token
//   const isMobile = /iPhone|iPad|iPod|Android/i.test(navigator.userAgent);

//   if (to.path === '/login') {
//     if (token) {
//       if (isMobile) {
//         // 如果是移动设备，则重定向到mobile页面
//         next({ name: 'mobile' });
//       } else {      // 如果已经登录，则重定向到app2或其他主页
//         if(userId!=='1001'){
//           next({ name: 'mobile' });
//         }else{
//           next({ name: 'app3' });
//         }
//       }

//     } else {
//       // 如果未登录，则允许访问登录页
//       next();
//     }
//   } else if (!token){
//     next({ name: 'login' });
//   }else{
//     next();
//   }
// });